<?php
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace App\Services;

use App\Models\ItemCateModel;
use App\Models\ItemModel;

/**
 * 栏目管理-服务类
 * @author 牧羊人
 * @since 2020/11/11
 * Class ItemCateService
 * @package App\Services
 */
class ItemCateService extends BaseService
{
    /**
     * 构造函数
     * @author 牧羊人
     * @since 2020/11/11
     * ItemCateService constructor.
     */
    public function __construct()
    {
        $this->model = new ItemCateModel();
    }

    /**
     * 获取栏目列表
     * @return array
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function getList()
    {
        // 查询条件
        $map = [];
        $list = $this->model->getList($map, [['sort', 'asc']]);
        return message("操作成功", true, $list);
    }

    /**
     * 添加或编辑
     * @return array
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function edit()
    {
        // 参数
        $data = request()->all();
        // 封面地址
        $cover = trim($data['cover']);
        if (!$data['id'] && !$cover) {
            return message('请上传栏目封面', false);
        }
        if (strpos($cover, "temp")) {
            $data['cover'] = save_image($cover, 'itemcate');
        } else {
            $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
        }
        return parent::edit($data); // TODO: Change the autogenerated stub
    }

    /**
     * 获取栏目列表
     * @return array
     * @since 2021/6/26
     * @author 牧羊人
     */
    public function getCateList()
    {
        // 获取栏目列表
        $cateList = $this->model
            ->where("status", "=", 1)
            ->where("mark", "=", 1)
            ->get()
            ->toArray();
        if (!empty($cateList)) {
            // 获取站点列表
            $itemModel = new ItemModel();
            $list = $itemModel
                ->where("status", "=", 1)
                ->where("mark", "=", 1)
                ->get()
                ->toArray();
            $itemList = array_column($list, "name", "id");
            foreach ($cateList as &$val) {
                $val['title'] = "【{$itemList[$val['item_id']]}】{$val['name']}";
            }
        }
        return message("操作成功", true, $cateList);
    }

}
